package ustc.sse;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;


public class SocketConnectionManagement {
	protected static Socket shortSocket = null;
	protected static Socket longSocket = null;
	public static final int timeOut = 2000;
	
	
	public static boolean establishConnection(String ipAddress, int port) {
		try {

			shortSocket = new Socket();
			SocketAddress socketAddress = new InetSocketAddress(ipAddress, port);
			shortSocket.connect(socketAddress, timeOut);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public static boolean establishLongConnection(String ipAddress, int port) {
		try {


			longSocket = new Socket();
			SocketAddress socketAddress = new InetSocketAddress(ipAddress, port);
			longSocket.connect(socketAddress);
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}


	public static Socket getShortInstance() {
		return shortSocket;
	}
	public static Socket getLongInstance() {
		return longSocket;
	}
	
	public static void closeshortSocket(){
		if(shortSocket != null){
			try {
				shortSocket.close();
				shortSocket = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static void closelongSocket(){
		if(longSocket != null){
			try {
				longSocket.close();
				longSocket = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
